package leetcode.offer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * ClassName: Offer61_IsStraight
 * Description: leetcode-剑指offer 61 扑克牌中的顺子
 * date: 2021-11-11 10:01
 *
 * @author liyifan
 */
public class Offer61_IsStraight {
    public boolean isStraight(int[] nums) {
        int zeroCount = 0;
        Arrays.sort(nums);
        for (int i = 0;i < 5;i++) {
            if (nums[i] == 0)
                zeroCount++;
            else
                break;
        }

        for (int i = zeroCount;i < 4;i++) {
            if (nums[i + 1] == nums[i])
                return false;
            int diff = nums[i + 1] - nums[i] - 1;
            if (diff == 0) {
                continue;
            } else if(diff > zeroCount) {
                return false;
            } else {
                zeroCount -= diff;
            }
        }
        return true;
    }

    public boolean isStraight2(int[] nums) {
        Set<Integer> repeat = new HashSet<>();
        int max = 0;
        int min = 14;
        for (int num : nums) {
            if (num == 0)
                continue;
            if (repeat.contains(num))
                return false;
            min = Math.min(min, num);
            max = Math.max(max, num);
            repeat.add(num);
        }
        return max - min < 5;
    }
}
